package com.xxx.car.web.admin;

import com.github.pagehelper.Page;
import com.xxx.car.entity.vo.request.ReqCouponTpl;
import com.xxx.car.entity.vo.request.ReqCouponTplQueryForAdmin;
import com.xxx.car.entity.vo.response.RespCouponTpl;
import com.xxx.car.service.CouponService;
import com.xxx.common.base.enums.AdminErrorEnum;
import com.xxx.common.base.vo.AdminResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

/**
 * Created by wenjiexu on 2017/7/5.
 */
@Controller
@RequestMapping("/admin/coupon")
public class AdminCouponController {

    @Autowired
    private CouponService couponService;

    @RequestMapping("/")
    public String index() {
        return "coupon";
    }

    @RequestMapping("/tplList")
    @ResponseBody
    public AdminResponse tplList(@Valid @ModelAttribute ReqCouponTplQueryForAdmin reqCouponTplQueryForAdmin, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            AdminResponse.returnFail(AdminErrorEnum.PARAMETER_INVALID);
        }
        Page<RespCouponTpl> res = couponService.queryCouponTpl(reqCouponTplQueryForAdmin);
        return AdminResponse.returnSuccess(res.getPageNum(), res.getPageSize(), res.getPages(), res.getTotal(), res.getResult());
    }

    @RequestMapping("/genCoupon")
    @ResponseBody
    public AdminResponse genCoupon(@Valid @ModelAttribute ReqCouponTpl reqCouponTpl, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            AdminResponse.returnFail(AdminErrorEnum.PARAMETER_INVALID);
        }
        couponService.genCoupon(reqCouponTpl);
        return AdminResponse.returnSuccess();
    }

    @RequestMapping("/usageDetail")
    @ResponseBody
    public AdminResponse usageDetail() {
        return AdminResponse.returnSuccess();
    }
}
